package com.microtf.inscription.metaweblog.xmlrpc;

import org.apache.xmlrpc.common.XmlRpcController;
import org.apache.xmlrpc.common.XmlRpcStreamConfig;
import org.apache.xmlrpc.serializer.TypeSerializer;
import org.xml.sax.SAXException;

/**
 * 本程序是fix apache的原生StringSerializer的bug
 *
 * bug描述在某些情况下生成的String类未使用string标签进行包装，
 * 按照xml rpc的标准是应该进行包装。
 * 本程序修复此bug，修改要点重写write方法。
 *
 * <p>使用自义的TypeFactoryImpl代替原来的org.apache.xmlrpc.common.TypeFactoryImpl</p>
 *
 * @see org.apache.xmlrpc.common.TypeFactoryImpl
 * @see StringSerializer
 * @author guliuzhong
 * @since 1.0
 */
public class TypeFactoryImpl extends org.apache.xmlrpc.common.TypeFactoryImpl {
    /**
     * 修正BUG后的StringSerializer
     */
    private static final TypeSerializer STRING_SERIALIZER = new StringSerializer();
    /**
     * XmlRpcController实现
     */
    private final XmlRpcController controller;


    /**
     * Creates a new instance.
     *
     * @param pController The controller, which operates the type factory.
     */
    public TypeFactoryImpl(XmlRpcController pController) {
        super(pController);
        controller = pController;
    }


    /**
     * 重写apache有问题的getSerializer方法
     * @see TypeFactoryImpl
     * @param pConfig 配置
     * @param pObject 需要序列化的对象
     * @return 修正后的TypeSerializer
     * @throws SAXException 未配置apache扩展类型时，但是使用到此类数据时出异常错误，如Byte等。
     */
    @Override
    public TypeSerializer getSerializer(XmlRpcStreamConfig pConfig, Object pObject) throws SAXException {
        TypeSerializer serializer = super.getSerializer(pConfig, pObject);
        if (serializer instanceof org.apache.xmlrpc.serializer.StringSerializer) {
            serializer = STRING_SERIALIZER;
        }
        return serializer;
    }
}
